<template>
	<view>
		<template v-if="list.length">
			<ScrollView
				class="theme-page-bg padding-20"
				:contentHeight="contentHeight"
			>
				<template #content> 
					<view class="m-t-20" v-for="(item, index) in list" :key="index">
						<AddressItem :dataItem="item" @onEdit="onEdit" @onRadioChange="(e) => onRadioChange(index, e)"></AddressItem>
					</view>
				</template>
			</ScrollView>
		</template>
		<empty v-else :show="false"/>
		<view class="button-bottom padding-20 bg-w flex">
			<view class="button-plain" @click="onExit">退出管理</view>
			<view class="button-style flex-1 m-l-20" @click="toAddAdress">+ 添加收货地址</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import ScrollView from '@/components/accompany/scrollView/scrollView.vue';
	import AddressItem from '../components/adressItem/index.vue';
	import empty from '@/components/empty/index.vue';
	
	import { onLoad, onShow } from '@dcloudio/uni-app';
	import { ref, getCurrentInstance } from 'vue';
	import type { Ref } from 'vue'
	
	const { proxy } = getCurrentInstance() as any;
	
	const list = ref([
		// { name: '张三', mobile: '15666666666', province: '河南省', city: '郑州市', area: ' 管城回族区', detailAddress: '郑东商业中心', defaulted: true },
		// { name: '张三', mobile: '15666666666', province: '河南省', city: '郑州市', area: ' 管城回族区', detailAddress: '郑东商业中心', defaulted: false }
	])
	
	const contentHeight:Ref<number> = ref(0)
	
	const getAddressList = () => {
		const url = proxy.$API.getAddressList;
		proxy.$UNIHTTP.getData(url).then((res: any) => {
			console.log(res, "地址列表");
			if (res.code == 0) {
				list.value = res.data;
			} 			
		})
	}
	
	const onExit = () => uni.navigateBack();
	
	const toAddAdress = () => {
		uni.navigateTo({url: '/pages/accompany-tmp/my/addressEdit/addressEdit'});
	}
	
	const onEdit = (item) => {
		uni.navigateTo({url: `/pages/accompany-tmp/my/addressEdit/addressEdit?detail=${encodeURIComponent(JSON.stringify(item))}`});
	}
	
	const onRadioChange = (curr: number, e) => {
		list.value = list.value.map((item, index) => {
			if(curr === index){
				item.defaulted = true;
			}else{
				item.defaulted = false;
			}
			return item;
		})
	}
	
	onLoad(() => {
		console.log('onLoad')
		let sysInfo = uni.getStorageSync('sysInfo')
		contentHeight.value = sysInfo.screenHeight - sysInfo.statusBarHeight - 44 - 110/sysInfo.devicePixelRatio
		console.log(contentHeight);
	})
	
	onShow(() => {
		getAddressList();
	})
</script>

<style lang="scss" scoped>
	.button-bottom {
		position: fixed;
		height: 100rpx;
		bottom: 0rpx;
		right: 0;
		left: 0;
		view {
			height: 88rpx;
			line-height: 88rpx;
		}
		.button-plain {
			padding: 0 56rpx;
		}
	}
</style>
